package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.enums.InterviewStatusEnum;
import com.kobeliu.mapper.InterviewMapper;
import com.kobeliu.pojo.Interview;
import com.kobeliu.pojo.bo.CreateInterviewBO;
import com.kobeliu.service.InterviewService;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class InterviewServiceImpl extends BaseInfoProperties implements InterviewService {

    @Autowired
    private InterviewMapper interviewMapper;


    @Transactional
    @Override
    public String create(CreateInterviewBO bo) {

        Interview pending = new Interview();

        BeanUtils.copyProperties(bo,pending);
        pending.setStatus(InterviewStatusEnum.WAITING.type);
        interviewMapper.insert(pending);

        redis.increment(HR_INTERVIEW_RECORD_COUNTS+":"+bo.getHrUserId(),1);
        redis.increment(CAND_INTERVIEW_RECORD_COUNTS+":"+bo.getCandUserId(),1);

        return pending.getId();
    }

    @Override
    public Interview detail(String interviewId, String hrUserId, String companyId) {

        return interviewMapper.selectOne(new QueryWrapper<Interview>()
                .eq("id",interviewId)
                .eq("hr_user_id",hrUserId)
                .eq("company_id",companyId));

    }

    @Override
    public void updateInterviewStatus(String interviewId, InterviewStatusEnum status) {


        Interview pending = new Interview();
        pending.setId(interviewId);
        pending.setStatus(status.type);
        interviewMapper.updateById(pending);

        Interview temp = interviewMapper.selectById(interviewId);

        //累减
        redis.decrement(HR_INTERVIEW_RECORD_COUNTS+":"+temp.getHrUserId(),1);
        redis.decrement(CAND_INTERVIEW_RECORD_COUNTS+":"+temp.getCandUserId(),1);
    }

    @Override
    public PagedGridResult listOfHr(String hrId, String companyId, Integer page, Integer limit) {

        PageHelper.startPage(page,limit);

        List<Interview> interviews = interviewMapper.selectList(new QueryWrapper<Interview>()
                .eq("hr_user_id", hrId)
                .eq("company_id", companyId)
                .eq("status", InterviewStatusEnum.WAITING.type));

        return setterPagedGrid(interviews,page);
    }

    @Override
    public PagedGridResult listOfCand(String candUserId, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);

        List<Interview> interviews = interviewMapper.selectList(new QueryWrapper<Interview>()
                .eq("cand_user_id", candUserId)
                .eq("status", InterviewStatusEnum.WAITING.type));

        return setterPagedGrid(interviews,page);
    }
}
